import requests
from lxml import etree

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36',
}

color_data = {
    # 舒适度
    'indices-comf-level--1': '#95B359',
    'indices-comf-level--2': '#C7BC13',
    'indices-comf-level--3': '#FFC805',
    'indices-comf-level--4': '#F18720',
    'indices-comf-level--5': '#EC5252',
    'indices-comf-level--6': '#B145C6',
    'indices-comf-level--7': '#7746D6',
    # 洗车
    'indices-cw-level--1': '#95B359',
    'indices-cw-level--2': '#C7BC13',
    'indices-cw-level--3': '#EC5252',
    'indices-cw-level--4': '#B145C6',
    # 穿衣
    'indices-drsg-level--1': '#7746D6',
    'indices-drsg-level--2': '#B145C6',
    'indices-drsg-level--3': '#EC5252',
    'indices-drsg-level--4': '#F18720',
    'indices-drsg-level--5': '#FFC805',
    'indices-drsg-level--6': '#C7BC13',
    'indices-drsg-level--7': '#95B359',
    # 感冒
    'indices-flu-level--1': '#95B359',
    'indices-flu-level--2': '#FFC805',
    'indices-flu-level--3': '#EC5252',
    'indices-flu-level--4': '#B145C6',
    # 运动
    'indices-sport-level--1': '#95B359',
    'indices-sport-level--2': '#C7BC13',
    'indices-sport-level--3': '#EC5252',
    # 旅游
    'indices-trav-level--1': '#95B359',
    'indices-trav-level--2': '#C7BC13',
    'indices-trav-level--3': '#FFC805',
    'indices-trav-level--4': '#EC5252',
    'indices-trav-level--5': '#B145C6',
    # 紫外线
    'indices-uv-level--1': '#95B359',
    'indices-uv-level--2': '#C7BC13',
    'indices-uv-level--3': '#FFC805',
    'indices-uv-level--4': '#EC5252',
    'indices-uv-level--5': '#B145C6',
    # 空气污染
    'indices-air-level--1': '#95B359',
    'indices-air-level--2': '#A9A538',
    'indices-air-level--3': '#E0991D',
    'indices-air-level--4': '#D96161',
    'indices-air-level--5': '#A257D0',

}


def indices_detail(tree):
    data = []
    # 第一排
    for i in range(4):
        top = tree.xpath(f'/html/body/div[3]/div[3]/div/div[2]/div[1]/div[2]/div/div[1]/div[1]/div[{i + 1}]/p/text()')[
            0]
        text = tree.xpath(f'/html/body/div[3]/div[3]/div/div[2]/div[1]/div[2]/div/p[{i + 1}]/text()')[0]
        day4List = []
        for j in range(4):
            day = tree.xpath(
                f'/html/body/div[3]/div[3]/div/div[2]/div[1]/div[2]/div/div[{i + 2}]/div/div[{j + 1}]/div[1]/div/p[1]/text()')[
                0]
            date = tree.xpath(
                f'/html/body/div[3]/div[3]/div/div[2]/div[1]/div[2]/div/div[{i + 2}]/div/div[{j + 1}]/div[1]/div/p[2]/text()')[
                0]
            level = tree.xpath(
                f'/html/body/div[3]/div[3]/div/div[2]/div[1]/div[2]/div/div[{i + 2}]/div/div[{j + 1}]/div[2]/p[1]/text()')[
                0]
            color = tree.xpath(
                f'/html/body/div[3]/div[3]/div/div[2]/div[1]/div[2]/div/div[{i + 6}]/div/div[{j + 1}]/div[2]/p[1]/@class')[
                0]
            txt = tree.xpath(
                f'/html/body/div[3]/div[3]/div/div[2]/div[1]/div[2]/div/div[{i + 2}]/div/div[{j + 1}]/div[2]/p[2]/text()')[
                0]
            obj = {
                'day': day,
                'date': date,
                'level': level,
                'color': color_data.get(color.replace('indices-forecast__tag ','')),
                'txt': txt
            }
            day4List.append(obj)
        indices = {
            'top': top,
            'txt': text,
            'day4List': day4List
        }
        data.append(indices)

    # 第二排
    for i in range(4):
        top = tree.xpath(f'/html/body/div[3]/div[3]/div/div[2]/div[1]/div[2]/div/div[1]/div[2]/div[{i + 1}]/p/text()')[
            0]
        text = tree.xpath(f'/html/body/div[3]/div[3]/div/div[2]/div[1]/div[2]/div/p[{i + 5}]/text()')[0]
        day4List = []
        for j in range(4):
            day = tree.xpath(
                f'/html/body/div[3]/div[3]/div/div[2]/div[1]/div[2]/div/div[{i + 6}]/div/div[{j + 1}]/div[1]/div/p[1]/text()')[
                0]
            date = tree.xpath(
                f'/html/body/div[3]/div[3]/div/div[2]/div[1]/div[2]/div/div[{i + 6}]/div/div[{j + 1}]/div[1]/div/p[2]/text()')[
                0]
            level = tree.xpath(
                f'/html/body/div[3]/div[3]/div/div[2]/div[1]/div[2]/div/div[{i + 6}]/div/div[{j + 1}]/div[2]/p[1]/text()')[
                0]
            color = tree.xpath(
                f'/html/body/div[3]/div[3]/div/div[2]/div[1]/div[2]/div/div[{i + 6}]/div/div[{j + 1}]/div[2]/p[1]/@class')[
                0]
            txt = tree.xpath(
                f'/html/body/div[3]/div[3]/div/div[2]/div[1]/div[2]/div/div[{i + 6}]/div/div[{j + 1}]/div[2]/p[2]/text()')[
                0]
            obj = {
                'day': day,
                'date': date,
                'level': level,
                'color': color_data.get(color.replace('indices-forecast__tag ','')),
                'txt': txt
            }
            day4List.append(obj)
        indices = {
            'top': top,
            'txt': text,
            'day4List': day4List
        }
        data.append(indices)
    return data


# 生活指数
def indicesHtml(city, code):
    weather_url = f'https://www.qweather.com/indices/{city}-{str(code)}.html'
    try:
        response = requests.get(url=weather_url, headers=headers)
        weather_tree = etree.HTML(response.text)

        data = indices_detail(weather_tree)
        # print(data)
        return data
    except:
        string = '爬取失敗,请检查输入的参数是否正确'
        print(string)
        return string


# 测试代码
# indicesHtml('guangzhou', 101280101)


